package weaver.interfaces.its.cronjob;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.interfaces.its.EmailUtil;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2023/12/4
 * @Description 删除人员账号生成流程
 * @Author Lee
 */
public class DueDateRemindCronJob extends BaseCronJob {
    private BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        RecordSet rs = new RecordSet();

        String workflowId = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_workflowId");
        String mainTable = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_mainTable");
        String detailTable = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_detailTable");
        String detailTable1 = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_detailTable1");
        String detailTable2 = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_detailTable2");
        String detailTable3 = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_detailTable3");
        String workflowUrl = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_workflowUrl");
        //duedate小于当前日期并且未done的发邮件提醒
        String sql = "select DISTINCT a.requestid,a.requestname,h.email as e,h1.email as e1,h2.email as e2,h3.email as e3\n" +
                "from workflow_requestbase a \n" +
                "LEFT JOIN " + mainTable + " c on c.requestid=a.requestid\n" +
                "LEFT JOIN " + detailTable + " d on d.mainid=c.id\n" +
                "LEFT JOIN " + detailTable1 + " d1 on d1.mainid=c.id\n" +
                "LEFT JOIN " + detailTable2 + " d2 on d2.mainid=c.id\n" +
                "LEFT JOIN " + detailTable3 + " d3 on d3.mainid=c.id\n" +
                "LEFT JOIN workflow_nownode e on e.requestid=a.requestid\n" +
                "LEFT JOIN  workflow_currentoperator f on f.requestid=e.requestid and nodeid=e.nownodeid\n" +
                "LEFT JOIN HRMRESOURCE h on h.id=c.sqr\n" +
                "LEFT JOIN HRMRESOURCE h1 on h1.id=c.sqrjl\n" +
                "LEFT JOIN HRMRESOURCE h2 on h2.id=c.zrr\n" +
                "LEFT JOIN HRMRESOURCE h3 on h3.id=c.zrrjl\n" +
                "where  a.workflowid=" + workflowId + " and a.currentnodetype!=3\n" +
                "and d.duedate<='" + TimeUtil.getCurrentDateString() + "' and " +
                "(d.zt !=2 or d.zt is null or d1.zt !=2 or d1.zt is null or d2.zt !=2 or d2.zt is null or d3.zt !=2 or d3.zt is null)";
        rs.execute(sql);
        while (rs.next()) {
            try {
                String requestid = rs.getString("requestid");
                String requestname = rs.getString("requestname");
                String email = rs.getString("e");
                String email1 = rs.getString("e1");
                String email2 = rs.getString("e2");
                String email3 = rs.getString("e3");
                List<String> strings = Arrays.asList(email, email1,email2,email3);
                String emails = strings.stream()
                        .filter(s -> s != null && !s.isEmpty())
                        .collect(Collectors.joining(","));
                Map<String, String> paramMap = new HashMap<>();
                paramMap.put("email", emails);
                String content = "<p>&nbsp; &nbsp; 您的流程DueDate到期未办结，请关注。 " +
                        "<a href=" + workflowUrl + requestid + ">" + requestname + "</a>";
                paramMap.put("content", content);
                paramMap.put("zt", "流程超时提醒(" + requestname + ")");
                EmailUtil.sendEmail(paramMap);
            } catch (Exception e) {
                baseBean.writeLog("DueDateRemindCronJob-" + e.getMessage());
            }
        }
    }
}
